package cc.rengu.oltp.service.common.dao.impl;

import cc.rengu.jradp.mods.xml.XmlTree;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.dao.SmsInfoMapper;
import cc.rengu.oltp.service.common.entity.SmsInfo;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 短信验证码表操作接口实现类
 * Created by 王成 on 2020/2/7
 */
public class SmsInfoMapperImpl implements SmsInfoMapper {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppParamConstant.OLTP_POOL_NAME);

    public SmsInfoMapperImpl() throws Exception {
        if (XmlTree.hasThreadTree()) {
            XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
            String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
            if (null != corporation && !corporation.isEmpty()) {
                String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
                dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
            }
        }
    }

    @Override
    public int insertSmsInfo(SmsInfo smsInfo) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertSmsInfo");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, smsInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记短信验证码信息失败,instId:<{}>,smsCode:<{}>,verifySeqNum:<{}>,iReturnCode:<{}>", smsInfo.getInstId(), smsInfo.getSmsCode(), smsInfo.getVerifySeqNum(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateSmsInfoByPrimaryKey(SmsInfo smsInfo) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateSmsInfoByPrimaryKey");
        smsInfo.setLastVerifyTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, smsInfo, null);
        if (0 != iReturnCode) {
            rglog.error("根据主键更新短信验证码信息失败,instId:<{}>,smsCode:<{}>,verifySeqNum:<{}>,iReturnCode:<{}>", smsInfo.getInstId(), smsInfo.getSmsCode(), smsInfo.getVerifySeqNum(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public SmsInfo selectSmsInfoByPrimaryKey(String instId, String verifySeqNum) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectSmsInfoByPrimaryKey");
        SmsInfo smsInfo = new SmsInfo();
        smsInfo.setInstId(instId);
        smsInfo.setVerifySeqNum(verifySeqNum);
        smsInfo.setSmsStatus(AppParamConstant.NO);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, smsInfo, smsInfo);
        if (0 != iReturnCode) {
            rglog.warn("根据主键短信验证码信息失败,instId:<{}>,verifySeqNum:<{}>,iReturnCode:<{}>", smsInfo.getInstId(), smsInfo.getVerifySeqNum(), iReturnCode);
            return null;
        }
        return smsInfo;
    }

    @Override
    public List<SmsInfo> selectSmsIfoByMobileNo(String instId, String mobileNo, String smsCreateTime) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectSmsIfoByMobileNo");
        SmsInfo smsInfo = new SmsInfo();
        smsInfo.setInstId(instId);
        smsInfo.setVerifyMobileNo(mobileNo);
        smsInfo.setSmsCreateTime(smsCreateTime);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, smsInfo, SmsInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.warn("根据手机号统计短信申请信息失败,instId:<{}>,mobileNo:<{}>,smsCreateTime:<{}>", instId, mobileNo, smsCreateTime);
            return null;
        }
        return objectList.stream().map(item -> (SmsInfo) item).collect(Collectors.toList());
    }
}
